const stateValue = {
    // 购物车商品
    cartGoods: {},
    // 购物车数量
    cartNumer: 0,
    // 购物车总价
    totalPrice: 0,

    // 显示商品选择
    showGoodSelect: false,
    // 显示商品规格
    showGoodSpec: false,
    // 当前选择商品
    currentGood: {},

    // 是否有桌号
    has_table_no: true,
    // 桌号
    table_no: "A01",
    // 用餐人数
    peopleNumber: 0,
    // 备注
    remark: "",

    // 支付方式
    payment: "weixin",

    // 当前选中的优惠券
    currentCoupon: null,
    // 当前选中的优惠券id
    coupon_id: 0,
};
let defauleValue = JSON.parse(JSON.stringify(stateValue));

export default {
    namespaced: true,
    state: stateValue,
    mutations: {
        set(state, data) {
            for (let key in data) {
                state[key] = data[key];
            }
        },
        reset(state) {
            for (let key in defauleValue) {
                state[key] = defauleValue[key];
            }
        },
        setCartStructure(state) {
            let cartNumer = 0;
            let totalPrice = 0;
            for (let key in state.cartGoods) {
                cartNumer += state.cartGoods[key].number;
                if (state.cartGoods[key].spceGoods != undefined) {
                    for (let skey in state.cartGoods[key].spceGoods) {
                        let specGood = state.cartGoods[key].spceGoods[skey];
                        totalPrice += specGood.currentGoodPrice * specGood.number;

                        if (specGood.number == 0) {
                            delete state.cartGoods[key].spceGoods[skey];
                        }
                    }
                } else {
                    totalPrice += state.cartGoods[key].number * state.cartGoods[key].goodPrice;
                }

                if (state.cartGoods[key].number == 0) {
                    delete state.cartGoods[key];
                }
            }
            state.cartNumer = cartNumer;
            state.totalPrice = totalPrice;
        },
    },
};
